<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="visable" title="重置密码" width="500px" @close="closeDlg">
    <el-form ref="pform" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="密码" prop="password">
        <el-input
          v-model.trim="form.password"
          maxlength="20"
          type="password"
          autocomplete="off"
          placeholder="长度不能少于6"
        />

      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model.trim="form.confirmPassword"
          type="password"
          maxlength="20"
          autocomplete="off"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
export default {
  name: 'ChangePasswordForm',
  data() {
    var equalPsw = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      isAdd: false,
      loading: false,
      visable: false,
      form: {
        password: '',
        confirmPassword: ''
      },
      rules: {
        password: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 6, message: '长度不能少于6', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '必填', trigger: 'blur' },
          { validator: equalPsw, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.closeDlg()
    },
    doSubmit() {
      this.$refs['pform'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.doChange()
        }
      })
    },
    change(id) {
      this.form.id = id
      this.form.password = ''
      this.form.confirmPassword = ''
      this.visable = true
      this.loading = false
    },
    doChange() {
      request({ url: '/sys/user/changePassword', method: 'post', data: this.form }).then(res => {
        this.closeDlg()
        this.$notify({
          title: '重置密码成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
      }).catch(err => {
        this.loading = false
        this.$message.error(err.message)
      })
    },
    closeDlg() {
      this.visable = false
      this.form.id = ''
    }
  }
}
</script>
